package dynamicJsonVerifyer.demo.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import dynamicJsonVerifyer.Const;
import dynamicJsonVerifyer.VerifyMetadataLoader;
import dynamicJsonVerifyer.VerifyMetadataSelecter;
import dynamicJsonVerifyer.demo.other.ProductFunction;
import dynamicJsonVerifyer.demo.other.RedisService;
import dynamicJsonVerifyer.demo.other.TslService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public class IotVaildMetadataLoader implements VerifyMetadataLoader {

    @Autowired
    TslService tslService;
    @Autowired
    RedisService redisService;

    public void load(VerifyMetadataSelecter selecter) {
        List<ProductFunction> list = tslService.all();
        load(selecter, list);
    }

    @Override
    public void renewal(VerifyMetadataSelecter selecter, String renewalKey, long versionTime) {

        /**
         * redis判断是否需要更新
         */

        List<ProductFunction> list = tslService.list(renewalKey);

        load(selecter, list);

    }

    private void load(VerifyMetadataSelecter selecter, List<ProductFunction> list){
        for(ProductFunction function : list){

            JSONObject json = JSONObject.parseObject(function.getJsonData());
            switch (function.getFunctionType()) {
                case "property":
                    load(selecter, function.getProductKey(), "iot."+function.getProductKey()+".property", "", json);
                    break;
                case "event":
                    for(Object datas : json.getJSONArray("outputData")){
                        load(selecter, function.getProductKey(),"iot."+function.getProductKey()+".event."+function.getIdentifier(), "", (JSONObject) datas);
                    }
                    break;
                case "service":
                    for(Object datas : json.getJSONArray("outputData")){
                        load(selecter, function.getProductKey(),"iot."+function.getProductKey()+".service."+function.getIdentifier(), "", (JSONObject) datas);
                    }
                    break;
            }
        }
    }

    private void load(VerifyMetadataSelecter selecter, String renewalKey, String mdKey, String prefix, JSONObject json) {
//        String mdKey = "iot."+productKey+".property";
        String id = json.get("identifier").toString();
        String dataType= json.get("dataType").toString();
        JSONObject metadataJson = new JSONObject();


        switch (dataType){
            case "struct":
                metadataJson.put(Const.VaildMetadataItem.dataType, Const.BaseVerifyDataType.struct);
                selecter.put(mdKey, renewalKey,prefix+id, metadataJson);
                JSONArray structDatas = json.getJSONArray("structData");
                for(Object structData : structDatas){
                    load(selecter, renewalKey, mdKey, prefix+id+".", (JSONObject) structData);
                }
                break;
            case "array":
                JSONObject arrayData = json.getJSONObject("arrayData");
                Integer size = arrayData.getInteger("size");
                metadataJson.put(Const.VaildMetadataItem.dataType, Const.BaseVerifyDataType.array);
                metadataJson.put(Const.VaildMetadataItem.arrayMaxSize, size);
                selecter.put(mdKey, renewalKey, prefix+id, metadataJson);
                for(int i=0; i<size; i++){
                    arrayData.put("identifier", i);
                    load(selecter, renewalKey, mdKey, prefix+id+".", arrayData);
                }
                break;
            case "enum":
                JSONObject enumDataJson = json.getJSONObject("enumData");
                String enumData = "";
                for(String key : enumDataJson.keySet()){
                    enumData += key+",";
                }
                metadataJson.put(Const.VaildMetadataItem.dataType, Const.BaseVerifyDataType.enum0);
                metadataJson.put(Const.VaildMetadataItem.enumData, enumData);
                selecter.put(mdKey, renewalKey,prefix+id, metadataJson);
                break;
            default:
                metadataJson.put(Const.VaildMetadataItem.dataType, dataType);
                selecter.put(mdKey, renewalKey,prefix+id, metadataJson);
                break;
        }
    }
}
